package com.example.shop_2208.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2208.entity.GoodsEntity
import com.example.shop_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class SearchViewModel:BaseViewModel() {
    //定义搜索成功的返回值 --接收的是一个list集合  （不包含code和message部分）
    val searchSuccess = MutableLiveData<List<GoodsEntity>>()
    val searchFail = MutableLiveData<String>()
    fun search(name:String){
        viewModelScope.launch {
            try {
                val result = apiServer.goods(0,1,100)
                val data = result.data.filter {
                    it.goods_desc.contains(name)
                }
                searchSuccess.value = data

            }catch (e:Exception){
                searchFail.value = e.message
            }
        }
    }


}